package ufabc.edu.helppo.rpc.command.impl;

import static ufabc.edu.helppo.rpc.RPCOpenSimMethods.ADMIN_BROADCAST;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.MENSAGEM;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.SERVER_PASSWORD;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

import ufabc.edu.helppo.rpc.command.RPCCommand;
import ufabc.edu.helppo.rpc.singleton.HelppoProperties;
import ufabc.edu.helppo.rpc.singleton.RpcClientSingleton;

public class AdminBroadcastRPCCommandImpl extends RPCCommand {
	private String mensagem;

	@Override
	public boolean execute() throws XmlRpcException, IOException {

		XmlRpcClient client = RpcClientSingleton.getInstance();
		configure(client);
		HashMap<String, Object> retorno = (HashMap<String, Object>) client
				.execute(ADMIN_BROADCAST, parameter());

		String resultado = "" + retorno.get("success");

		try {
			if (resultado.equalsIgnoreCase("True")) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	private List<Hashtable<String, String>> parameter() throws IOException {
		Hashtable<String, String> parameters = new Hashtable<String, String>();

		ResourceBundle properties = HelppoProperties.getInstance();

		parameters.put(MENSAGEM, mensagem);
		parameters.put(SERVER_PASSWORD, properties.getString(SERVER_PASSWORD));

		List<Hashtable<String, String>> list = new ArrayList<Hashtable<String, String>>();
		list.add(parameters);

		return list;
	}

	public String getMensagem() {
		return mensagem;
	}

	public void setMensagem(String mensagem) {
		this.mensagem = mensagem;
	}

}